package t327.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import t327.pojo.font.LuiTableEntity;
import t327.pojo.staff.Department;
import t327.pojo.staff.ReimbursementApplication;
import t327.pojo.staff.StaffManagement;
import t327.pojo.staff.StaffPunchCard;
import t327.pojo.staffView.StaffManagementView;
import t327.pojo.staffView.StaffPunchCardView;
import t327.service.iter.IStaffManageService;
import t327.util.BaiDuFaceUntil;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("staff")
public class StaffController {
    @Autowired
    private IStaffManageService staffManageService;

    private static final String ID_CARD_REGEX = "^(\\d{17}[0-9xX])|(\\d{15})$";
    private static final String regex = "^1[3456789]\\d{9}$";
    @RequestMapping("AddStaffController")
    public Map<String,String> AddStaffController(@RequestBody StaffManagement data){
        // 创建Pattern对象
        Pattern pattern = Pattern.compile(regex);
        // 进行匹配
        boolean isMatch = pattern.matcher(data.getStaffPhone().toString()).matches();
        Map<String,String> map = new HashMap<>();
        boolean isValid = false;
        if (data.getStaffAge()==null ){
            map.put("code", "400");
            map.put("msg","年龄不能为空");
            return map;
        }else if (data.getStaffAge()>0&&data.getStaffAge()>140){
            map.put("code", "400");
            map.put("msg","年龄不能为超过140或不能用负数");
            return map;
        }else if (data.getStaffName()=="" || data.getStaffName()==null){
            map.put("code", "400");
            map.put("msg","名字不能为空");
            return map;
        }else if (data.getStaffSex()!=1 && data.getStaffSex()!=0){
            map.put("code", "400");
            map.put("msg","性别有误");
            return map;
        }else if (data.getStaffIdentityCard()==null){
            map.put("code", "400");
            map.put("msg","身份证不能为空");
            return map;
        }else if (ID_CARD_REGEX.equals(data.getStaffIdentityCard())){
            map.put("code", "400");
            map.put("msg","身份证不符");
            return map;
        }
        else if (data.getStaffPhone()==null){
            map.put("code", "400");
            map.put("msg","手机号为空");
            return map;
        }else if (!isMatch){
            map.put("code", "400");
            map.put("msg","手机格式不对");
            return map;
        }
        else if (data.getStaffDepartmentId()==-1L){
            map.put("code", "400");
            map.put("msg","部门不能为空");
            return map;
        }else if (data.getStaffEducationBackground()==null){
            map.put("code", "400");
            map.put("msg","学历不能为空");
            return map;
        }else if(data.getStaffBankCard()==null){
            map.put("code", "400");
            map.put("msg","银行卡号不能为空");
            return map;
        }else if(!validateICBCCardNumber(data.getStaffBankCard()) ){
            map.put("code", "400");
            map.put("msg","银行卡号格式不对");
            return map;
        }
        else{
            if (staffManageService.addStaffManagement(data)>0){
                map.put("code", "200");
                map.put("msg","添加成功");
                return map;
            }
            map.put("code", "400");
            map.put("msg","添加失败");
            return map;
        }

    }
    @RequestMapping("selectStaffDepartment")
    public List<Department> selectStaffDepartment(){

        /*else if (staffManagementView.getStaffImg()==null){
            System.out.println("图片不能为空");
        }*/
        return staffManageService.selectStaffDepartment();
    }
    @RequestMapping("dimissionController")
    public Map<String, String> dimissionController(@RequestBody Map<String, Object> request) {
        Long id = Long.valueOf(request.get("id").toString());
        Map<String, String> stringStringMap = staffManageService.updateStaffResignationTime(id);
        return stringStringMap;
    }
    @RequestMapping("queryAll")
    public LuiTableEntity getSemInfo(@RequestParam(value = "page") Integer pageNum, @RequestParam(value = "limit")Integer pageSize){
/*
*     "code": res.status, //解析接口状态
      "msg": res.message, //解析提示文本
      "count": res.total, //解析数据长度
      "data": res.data.item //解析数据列表
* */
        PageHelper.startPage(pageNum,pageSize);
        LuiTableEntity luiTableEntity = new LuiTableEntity<StaffManagementView>();
        PageInfo<StaffManagementView> productSemesterViewPageInfo = new PageInfo<StaffManagementView>(staffManageService.queryAll());
        List<StaffManagementView> products = productSemesterViewPageInfo.getList();
        long total = productSemesterViewPageInfo.getTotal();
        if(products!=null&&products.size()>0){
            luiTableEntity.setCode("0");
            luiTableEntity.setMsg("查询成功");
            luiTableEntity.setCount(total);
            luiTableEntity.setData(products);
        }else{
            luiTableEntity.setCode("203");
            luiTableEntity.setMsg("查询失败");

        }
        System.out.println("1");
        System.out.println(products);
        return   luiTableEntity;
    }

    public static boolean validateICBCCardNumber(String cardNumber) {
        // 正则表达式验证工商银行卡号
        String regex = "^(\\d{16}|\\d{19})$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(cardNumber);
        return matcher.matches();
    }
    @RequestMapping("updateStaffResignationTime")
    public Map<String, String> dimissionController() {
        return  staffManageService.updateStaffResignationTime1();
    }
    @RequestMapping("selectIdStaff")
    public List<StaffManagement> selectIdStaff(Long id) {
        return  staffManageService.selectIdStaff(id);
    }

    public Map<String,String> updateStaff(@RequestBody StaffManagement data, Long id){
        // 创建Pattern对象
        Pattern pattern = Pattern.compile(regex);
        // 进行匹配
        boolean isMatch = pattern.matcher(data.getStaffPhone().toString()).matches();
        Map<String,String> map = new HashMap<>();
        boolean isValid = false;
        if (data.getStaffAge()==null ){
            map.put("code", "400");
            map.put("msg","年龄不能为空");
            return map;
        }else if (data.getStaffAge()>0&&data.getStaffAge()>140){
            map.put("code", "400");
            map.put("msg","年龄不能为超过140或不能用负数");
            return map;
        }else if (data.getStaffName()=="" || data.getStaffName()==null){
            map.put("code", "400");
            map.put("msg","名字不能为空");
            return map;
        }else if (data.getStaffSex()!=1 && data.getStaffSex()!=0){
            map.put("code", "400");
            map.put("msg","性别有误");
            return map;
        }else if (data.getStaffIdentityCard()==null){
            map.put("code", "400");
            map.put("msg","身份证不能为空");
            return map;
        }else if (ID_CARD_REGEX.equals(data.getStaffIdentityCard())){
            map.put("code", "400");
            map.put("msg","身份证不符");
            return map;
        }
        else if (data.getStaffPhone()==null){
            map.put("code", "400");
            map.put("msg","手机号为空");
            return map;
        }else if (!isMatch){
            map.put("code", "400");
            map.put("msg","手机格式不对");
            return map;
        }
        else if (data.getStaffDepartmentId()==-1L){
            map.put("code", "400");
            map.put("msg","部门不能为空");
            return map;
        }else if (data.getStaffEducationBackground()==null){
            map.put("code", "400");
            map.put("msg","学历不能为空");
            return map;
        }else if(data.getStaffBankCard()==null){
            map.put("code", "400");
            map.put("msg","银行卡号不能为空");
            return map;
        }else if(!validateICBCCardNumber(data.getStaffBankCard()) ){
            map.put("code", "400");
            map.put("msg","银行卡号格式不对");
            return map;
        }
        else{
            if (staffManageService.addStaffManagement(data)>0){
                map.put("code", "200");
                map.put("msg","添加成功");
                return map;
            }
            map.put("code", "400");
            map.put("msg","添加失败");
            return map;
        }

    }
    @RequestMapping("updateStaffController")
    public int updateStaff(@RequestBody StaffManagement staff) {
        return  staffManageService.updateStaff(staff);
    }
    @RequestMapping("sendController")
    public Map<String,String> send(@RequestBody Map<String,String> map){
        double score=0;
/*        // 创建StringBuilder对象
        StringBuilder stringBuilder = new StringBuilder("");
        stringBuilder.append("");
       // 获取拼接后的字符串
        String resultString = stringBuilder.toString();*/
        Map<String,String> resultMap = new HashMap<>();
        //创建百度的对象
        BaiDuFaceUntil baiDuFaceUntil = new BaiDuFaceUntil();
        //获取前端的照片
        String image1 = map.get("image1");
        //数据库给img赋值
        String image2 =null;
        //查询所有在职和代理制的人员
        List<Long> longs = staffManageService.queryAllId();
        //遍历循环输出id
        for (int i = 0; i <longs.size(); i++) {
            Long integer = longs.get(i);
            List<String> strings = staffManageService.queryImg(integer);
            image2 = strings.get(0).substring(strings.get(0).indexOf(',') + 1);
            //对比数据库的人脸
            JSONObject result = baiDuFaceUntil.faceCompare(image1, image2);
            if (result==null){
                resultMap.put("code", "300");
                resultMap.put("msg", "人脸接口调用失败");
                return resultMap;
            }
            //拿到人脸的相似度
            try{
                JSONObject result1 = result.getJSONObject("result");
                score = result1.getDouble("score");
            }catch (Exception e){
                resultMap.put("code", "400");
                resultMap.put("msg", "没有找到您");
                return resultMap;
            }
            if (score>80){
                /*  StaffPunchCard staffPunchCard = new StaffPunchCard();*/
                List<StaffManagement> staffManagements = staffManageService.selectIdStaff(integer);
                StaffManagement staffManagement = staffManagements.get(0);
                String staffName = staffManagement.getStaffName();
                Long staffId = staffManagement.getStaffId();
                StaffPunchCard staffPunchCard = new StaffPunchCard();
                Date printTime = staffPunchCard.getPrintTime();
                int addPunchCardIsOk = staffManageService.addPunchCard(staffId,printTime);
                if (addPunchCardIsOk<0){
                    resultMap.put("code", "400");
                    resultMap.put("msg", "打卡失败");
                    return resultMap;
                }
                resultMap.put("code", "200");
                resultMap.put("msg", "欢迎"+" "+staffName);
                return resultMap;
            }
        }
        resultMap.put("code", "400");
        resultMap.put("msg", "没有找到您");
        return resultMap;
    }
    @RequestMapping("addStaffPunchCardTwoController")
    public Map<String,String> addStaffPunchCardTwoController(){
        Map<String,String> map = new HashMap<>();
        if ( staffManageService.addStaffPunchCardTwo()){
            map.put("code", "200");
            map.put("msg", "ok");
            return map;
        }
        map.put("code", "400");
        map.put("msg", "错误");
        return map;
    }
    @RequestMapping("queryStaffPunchCardView")
    public LuiTableEntity queryStaffPunchCardView(@RequestParam(value = "page") Integer pageNum, @RequestParam(value = "limit")Integer pageSize){
/*
*     "code": res.status, //解析接口状态
      "msg": res.message, //解析提示文本
      "count": res.total, //解析数据长度
      "data": res.data.item //解析数据列表
* */
        PageHelper.startPage(pageNum,pageSize);
        LuiTableEntity luiTableEntity = new LuiTableEntity<StaffPunchCardView>();
        PageInfo<StaffPunchCardView> productSemesterViewPageInfo = new PageInfo<StaffPunchCardView>(staffManageService.queryStaffPunchCardView());
        List<StaffPunchCardView> products = productSemesterViewPageInfo.getList();
        long total = productSemesterViewPageInfo.getTotal();
        if(products!=null&&products.size()>0){
            luiTableEntity.setCode("0");
            luiTableEntity.setMsg("查询成功");
            luiTableEntity.setCount(total);
            luiTableEntity.setData(products);
        }else{
            luiTableEntity.setCode("203");
            luiTableEntity.setMsg("查询失败");

        }
        System.out.println("1");
        System.out.println(products);
        return   luiTableEntity;
    }
    @RequestMapping("updateByThePieceController")
    public Map<String,String> updateByThePiece(@RequestParam("id")Long id, @RequestParam("staffNumberOfCases")Long staffNumberOfCases){
        Map<String,String> map = new HashMap<>();
        if (staffManageService.updateByThePiece(id,staffNumberOfCases)>0){
            map.put("code", "200");
            map.put("msg", "修改成功");
            return map;
        }else{
            map.put("code", "400");
            map.put("msg", "修改失败");
            return map;
        }
    }
    @RequestMapping("addReimbursementApplicationController")
    public Map<String,String> addReimbursementApplicationController(@RequestBody ReimbursementApplication reimbursementApplication){
        Map<String,String> map = new HashMap<>();
        if (reimbursementApplication.getFeeName()==null||reimbursementApplication.getFeeName()==""){
            map.put("code", "300");
            map.put("msg", "费用名不能为空！");
            return map;
        }else if (reimbursementApplication.getReimbursementDate()==null){
            map.put("code", "300");
            map.put("msg", "报销日期不能为空！");
            return map;
        }else if (reimbursementApplication.getAmountOfReimbursement()==null ){
            map.put("code", "300");
            map.put("msg", "金额不能为空！");
            return map;
        }/*else if (onlyDigits){
            map.put("code", "300");
            map.put("msg", "金额只能是数字！");
            return map;
        }*/else if (reimbursementApplication.getReimbursementDepartmentId()==-1L){
            map.put("code", "300");
            map.put("msg", "部门不能为空");
            return map;
        }else if (reimbursementApplication.getAuditor()==null ||reimbursementApplication.getAuditor()==""){
            map.put("code", "300");
            map.put("msg", "审核人不能为空");
            return map;
        }else if (reimbursementApplication.getChargeUnit()==null ||reimbursementApplication.getChargeUnit()==""){
            map.put("code", "300");
            map.put("msg", "收费单位不能为空");
            return map;
        }else if (reimbursementApplication.getReimbursementApplicant()==null ||reimbursementApplication.getReimbursementApplicant()==""){
            map.put("code", "300");
            map.put("msg", "报销人不能为空");
            return map;
        }else if (reimbursementApplication.getRemark()==null ||reimbursementApplication.getRemark()==""){
            map.put("code", "300");
            map.put("msg", "补充说明不能为空");
            return map;
        } else{
            if (staffManageService.addReimbursementApplication(reimbursementApplication)){
                map.put("code", "200");
                map.put("msg", "提交成功");
                return map;
            }else{
                map.put("code", "400");
                map.put("msg", "修改失败");
                return map;
            }
        }


    }
    @RequestMapping("updateReissueCardController")
    public Map<String,String> updateReissueCardController(@RequestBody StaffPunchCardView data){
        Map<String,String> map  = new HashMap<>();
       if (staffManageService.updateReissueCard(data)){
           map.put("code", "200");
           map.put("msg", "提交成功");
           return map;
       }else {
           map.put("code", "400");
           map.put("msg", "无法修改");
           return map;
       }
    }
    public static boolean isStringOnlyDigits(String str) {
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }
    @RequestMapping("queryStaffPunchCardViewWhere")
    public LuiTableEntity queryStaffPunchCardViewWhere(@RequestParam(value = "page") Integer pageNum, @RequestParam(value = "limit")Integer pageSize, @RequestParam(value = "name")String name){
/*
*     "code": res.status, //解析接口状态
      "msg": res.message, //解析提示文本
      "count": res.total, //解析数据长度
      "data": res.data.item //解析数据列表
* */
        PageHelper.startPage(pageNum,pageSize);
        LuiTableEntity luiTableEntity = new LuiTableEntity<StaffPunchCardView>();
        PageInfo<StaffPunchCardView> productSemesterViewPageInfo = new PageInfo<StaffPunchCardView>(staffManageService.queryStaffPunchCardViewWhere(name));
        List<StaffPunchCardView> products = productSemesterViewPageInfo.getList();
        long total = productSemesterViewPageInfo.getTotal();
        if(products!=null&&products.size()>0){
            luiTableEntity.setCode("0");
            luiTableEntity.setMsg("查询成功");
            luiTableEntity.setCount(total);
            luiTableEntity.setData(products);
        }else{
            luiTableEntity.setCode("203");
            luiTableEntity.setMsg("查询失败");

        }
        System.out.println("1");
        System.out.println(products);
        return   luiTableEntity;
    }

}

